﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LiteExpress.Utilities
{
    public class CookieHelper
    {
        #region cookie operation
        public static void SetCookie(string name, string valu)
        {
            SetCookie(name, valu, null, null);
        }

        public static void SetCookie(string name, string valu, string domain)
        {
            SetCookie(name, valu, domain, null);
        }
        public static void SetCookie(string name, string valu, DateTime expires)
        {
            SetCookieWithTime(name, valu, null, expires);
        }
        public static void SetCookie(string name, string valu, TimeSpan expireIn)
        {
            SetCookie(name, valu, null, expireIn);
        }

        public static void SetCookie(string name, string value, string domain, TimeSpan? expireIn, bool withRequest = false, bool httpOnly = true)
        {
            HttpContext context = HttpContext.Current;
            if (context == null) return;

            HttpCookie cookie = context.Request.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name, HttpUtility.UrlEncode(value, Encoding.UTF8));
            }
            else
            {
                cookie.Value = HttpUtility.UrlEncode(value, Encoding.UTF8);
            }

            if (expireIn != null)
            {
                cookie.Expires = DateTime.Now.Add(expireIn.Value);                
            }

            cookie.HttpOnly = httpOnly;
            
            if (!string.IsNullOrEmpty(domain))
                cookie.Domain = domain;
            cookie.Path = "/";

            context.Response.Cookies.Set(cookie);
            if (withRequest)
            {
                context.Request.Cookies.Set(cookie);
            }
        }

        public static void SetCookieWithTime(string name, string value, string domain, DateTime expireAt)
        {
            SetCookie(name, value, domain, expireAt - DateTime.Now);
        }

        public static void SetCookieValues(string name
            , System.Collections.Specialized.NameValueCollection values, string domain
            , TimeSpan? expireIn, bool withRequest = false, bool httpOnly = true)
        {
            HttpContext context = HttpContext.Current;
            if (context == null) return;

            HttpCookie cookie = context.Request.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);                
            }
            
            cookie.Values.Clear();
            foreach (var item in values.AllKeys)
            {
                cookie.Values.Add(item, HttpUtility.UrlEncode(values[item], Encoding.UTF8));
            }

            if (expireIn != null)
            {
                cookie.Expires = DateTime.Now.Add(expireIn.Value);
            }
            cookie.HttpOnly = httpOnly;

            if (!string.IsNullOrEmpty(domain))
                cookie.Domain = domain;
            cookie.Path = "/";

            context.Response.Cookies.Set(cookie);
            if (withRequest)
            {
                context.Request.Cookies.Set(cookie);
            }
        }

        public static string GetCookie(string name)
        {
            string valu = string.Empty;
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                HttpCookie cookie = context.Request.Cookies[name];
                if (cookie != null)
                {
                    valu = HttpUtility.UrlDecode(cookie.Value, Encoding.UTF8);
                }
            }
            return valu;
        }

        public static void RemoveCookie(string name)
        {
            SetCookie(name, string.Empty, null, new TimeSpan(-1,0,0));
        }
        public static void RemoveCookie(string name, string domain, bool withRequest = false)
        {
            SetCookie(name, string.Empty, domain, new TimeSpan(-1, 0, 0));
            if (withRequest)
            {
                HttpContext.Current.Request.Cookies.Remove(name);
            }
        }

        public static void ClearCookie()
        {
            if (HttpContext.Current != null)
            {
                foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
                {
                    RemoveCookie(key);
                }
            }
        }
        #endregion
    }
}
